var gulp = require('gulp'),
    runSequence = require('run-sequence'),
    autoprefixer = require('gulp-autoprefixer'),
    jshint = require('gulp-jshint'),
    uglify = require('gulp-uglify'),
    imagemin = require('gulp-imagemin'),
    clean = require('gulp-clean'),
    notify = require('gulp-notify'),
    cache = require('gulp-cache'),
    connect = require('gulp-connect'),
    replace = require('gulp-replace'),
    htmlmin = require('gulp-htmlmin'),
    livereload = require('gulp-livereload'),
    stripDebug = require('gulp-strip-debug'),
    rev = require('gulp-rev'),
    //- 对文件名加MD5后缀
    revCollector = require('gulp-rev-collector'),
    less = require('gulp-less'),
    concat = require('gulp-concat'),
    rename = require('gulp-rename'),
    plumber = require('gulp-plumber'),
    minifycss = require('gulp-minify-css');

gulp.task('less', function() {
    gulp.src('app/less/**/*.less')
        .pipe(plumber())
        .pipe(less({
            compress: true
        }))
        .on('error', function(e) {
            console.log(e);
        })
        .pipe(gulp.dest('dist/css/'));

});

// 合并、压缩、重命名css
gulp.task('css', ['less'], function() {
    gulp.src(['./public/css/*.css'])
        .pipe(concat('all.css')) // 合并文件为all.css
        .pipe(gulp.dest('./public/css/')) // 输出all.css文件
        .pipe(rename({
            suffix: '.min'
        })) // 重命名all.css为 all.min.css
        .pipe(minifycss()) // 压缩css文件
        .pipe(gulp.dest('./public/css/')); // 输出all.min.css
});


// 开发版的脚本
gulp.task('scripts', function() {
    return gulp.src('app/scripts/**/*.js')
        // .pipe(concat('all.js'))
        .pipe(gulp.dest('dist/scripts'))
        //清除console
        // .pipe(stripDebug())
        // .pipe(rename({
        //     suffix: '.min'
        // }))
        // .pipe(uglify())
        
        
        /*.pipe(rev())
        .pipe(gulp.dest('dist/scripts/'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('dist/rev/scripts/'));*/

});
// 文件名后缀
gulp.task('rev', function() {
    gulp.src(['dist/rev/**/*.json', 'dist/index.html']) //- 读取 rev-manifest.json 文件以及需要进行css名替换的文件
        .pipe(revCollector()) //- 执行文件内后缀名的替换
        .pipe(gulp.dest('dist/')); //- 替换后的文件输出的目录
});
// 图片
gulp.task('images', function() {
    return gulp.src(['app/imgs/**/**'])
        // .pipe(cache(imagemin({
        //     optimizationLevel: 3,
        //     progressive: true,
        //     interlaced: true
        // })))
        .pipe(gulp.dest('dist/imgs'));
});
//
gulp.task('icon', function() {
    return gulp.src(['app/*.ico'])
        .pipe(cache(imagemin({
            optimizationLevel: 3,
            progressive: true,
            interlaced: true
        })))
        .pipe(gulp.dest('dist'));
});
//font
gulp.task('font', function() {
    return gulp.src('app/font/**/**')
        .pipe(gulp.dest('dist/font'));
});

//bower
gulp.task('bower', function() {
    return gulp.src("bower_components/**/**/**/**")
        .pipe(gulp.dest("dist/bower_components"));
});

//html
gulp.task('views', function() {
    return gulp.src("app/**/**/**/*.html")
        .pipe(htmlmin({
            removeComments: true, //清除HTML注释
            collapseWhitespace: true, //压缩HTML
            collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
            removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
            removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
            removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
            minifyJS: true, //压缩页面JS
            minifyCSS: true //压缩页面CSS
        }))
        .pipe(revCollector())
        .pipe(gulp.dest("dist/"));
});

// 清理
gulp.task('clean', function() {
    return gulp.src(['dist/*'], {
            read: false
        })
        .pipe(clean());
});

// 预设任务
gulp.task('default', ['clean'], function() {
    gulp.start('scripts', 'images', 'views', 'font');
});

// 看守
gulp.task('watch', function() {

    // 看守所有.scss档
    gulp.watch('app/less/**/*.less', ['less']);

    // 看守所有.js档
    gulp.watch('app/scripts/**/*.js', ['scripts']);
    // 看守所有图片档
    gulp.watch('app/imgs/**/*', ['images']);
    //看守所有字体
    gulp.watch('app/font/**', ['font']);

    // 看守所有的页面
    gulp.watch('app/**/**/**/**.html', ['views']);
    //看守首页
    gulp.watch('app/index.html', ['views']);

    // 建立即时重整伺服器
    var webServer = livereload();

    // 看守所有位在 dist/  目录下的档案，一旦有更动，便进行重整
    gulp.watch(['dist/**']).on('change', function(file) {
        livereload.reload("dist/index.html");
    });

});


gulp.task('build', function(done) {
    runSequence(['images'], ['font'], ['bower'], ['scripts'], ['views'], ['css'], ['icon'], done);
});


// 启动服务
gulp.task('serve', ['build', 'watch'], function() {
    connect.server({
        root: 'dist',
        prot: '8080',
        livereload: true
    });
});
